package com.ling.service;

import com.ling.bean.StarBlog;
import com.ling.bean.StarBlogExample;
import com.ling.dao.StarBlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StarBlogService {
    private final StarBlogMapper starBlogMapper;

    @Autowired
    public StarBlogService(StarBlogMapper starBlogMapper) {
        this.starBlogMapper = starBlogMapper;
    }

    //获取用户收藏的所有博客
    public List<StarBlog> getUserStarBlogsByUserId(String userId) {
        return starBlogMapper.selectStarBlogsByUserId(userId);
    }

    //是否已收藏
    public boolean hasStared(StarBlog starBlog) {
        StarBlogExample starBlogExample = new StarBlogExample();
        starBlogExample.createCriteria().andUseridEqualTo(starBlog.getUserid()).andStarblogidEqualTo(starBlog.getStarblogid());
        List<StarBlog> starBlogs = starBlogMapper.selectByExample(starBlogExample);
        return !starBlogs.isEmpty();
    }

    //写入
    public void saveStarBlog(StarBlog starBlog) {
        starBlogMapper.insertSelective(starBlog);
    }

    //取消收藏
    public void cancelStarBlog(String userId,String blogId) {
        StarBlogExample starBlogExample = new StarBlogExample();
        starBlogExample.createCriteria().andUseridEqualTo(userId).andStarblogidEqualTo(blogId);
        starBlogMapper.deleteByExample(starBlogExample);
    }
}
